package com.weixin.utils;

import com.thoughtworks.xstream.XStream;
import com.weixin.bean.MessageBean;
import com.weixin.bean.News;
import com.weixin.bean.NewsMessage;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * Created by Administrator on 2018-01-16.
 */
public class MessageUtils {

    public static final String MESSAGE_TEXT = "text";
    public static final String MESSAGE_IMAGE = "image";
    public static final String MESSAGE_VOICE = "voice";
    public static final String MESSAGE_VIDEO = "video";
    public static final String MESSAGE_SHORTVIDEO = "shortviedo";
    public static final String MESSAGE_LINK = "link";
    public static final String MESSAGE_LOCATION = "location";
    public static final String MESSAGE_EVENT = "event";
    public static final String MESSAGE_SUBSCRIBE = "subscribe";
    public static final String MESSAGE_UNSUBSCRIBE = "nusubscribe";
    public static final String MESSAGE_CLICK = "click";
    public static final String MESSAGE_VIEW = "view";
    public static final String MESSAGE_SCAN = "scan";
    public static final String MESSAGE_NEWS = "news";

    /**

     * 新建方法，将接收到的XML格式，转化为Map对象

     * @param request 将request对象，通过参数传入

     * @return 返回转换后的Map对象

     */

    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {

        Map<String, String> map = new HashMap<String, String>();

//从dom4j的jar包中，拿到SAXReader对象。

        SAXReader reader = new SAXReader();



        InputStream is = request.getInputStream();//从request中，获取输入流
        InputStreamReader read = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(read);
        String str = br.readLine();
        System.out.println(str);
        Document doc = DocumentHelper.parseText(str);
       // Document doc =  reader.read(is);//从reader对象中,读取输入流

        Element root = doc.getRootElement();//获取XML文档的根元素

        List<Element> list = root.elements();//获得根元素下的所有子节点

        for (Element e : list) {

            map.put(e.getName(), e.getText());//遍历list对象，并将结果保存到集合中

        }



        is.close();

        return map;

    }



    /**

     * 将文本消息对象转化成XML格式

     * @param message 文本消息对象

     * @return 返回转换后的XML格式

     */

    public static String objectToXml(MessageBean message){

        XStream xs = new XStream();

//由于转换后xml根节点默认为class类，需转化为<xml>

        xs.alias("xml", message.getClass());

        return xs.toXML(message);

    }

    /**
     * 将图文消息对象转成XML
     * @param
     * @return
     */
    public static String newsMessageToXml(NewsMessage newsMessage){
        XStream xstream = new XStream();
        //将xml的根节点替换成<xml>  默认为NewsMessage的包名
        xstream.alias("xml", newsMessage.getClass());
        //同理，将每条图文消息News类的报名，替换为<item>标签
        xstream.alias("item", new News().getClass());
        return xstream.toXML(newsMessage);
    }


    /**
     * 初始化图文消息
     */
    public static String initNewsMessage(String toUSerName,String fromUserName){
        List<News> newsList = new ArrayList<News>();
        NewsMessage newsMessage = new NewsMessage();
        //组建一条图文↓ ↓ ↓
        News newsItem = new News();
        newsItem.setTitle("欢迎来到杰瑞教育");
        newsItem.setDescription("杰瑞教育，中国高端互联网人才培训领导品牌！");
        newsItem.setPicUrl("http://www.jredu100.com/statics/images/index/top/logo.png");
        newsItem.setUrl("www.jredu100.com");
        newsList.add(newsItem);

        //组装图文消息相关信息
        newsMessage.setToUserName(fromUserName);
        newsMessage.setFromUserName(toUSerName);
        newsMessage.setCreateTime(new Date().getTime() + "");
        newsMessage.setMsgType(MESSAGE_NEWS);
        newsMessage.setArticle(newsList);
        newsMessage.setArticleCount(newsList.size());

        //调用newsMessageToXml将图文消息转化为XML结构并返回
        return MessageUtils.newsMessageToXml(newsMessage);
    }
}
